#!/usr/bin/python3
from .Base_Plugin import Base_Plugin
import os, json


class Initialization_Check(Base_Plugin):
    """
    初始化检查
    """

    def __init__(self, context) -> None:
        super().__init__()
        self.context = context
        self.config_filename = ".db_manager.json"

    def check_config_file(self, workspace, source):
        """
        检测配置文件
        """
        with open(
            os.path.join(os.path.expanduser("~"), self.config_filename),
            mode="r",
            encoding="utf-8",
        ) as f:
            configs = json.loads(f.read())
            config = list(
                filter(lambda item: item["enable"] == 1, configs["projects"])
            ).pop()
            author = config["author"]
            package = config["package"]
            assert workspace is not None, self.error_log(
                "未读取到workspace（工作空间）配置"
            )
            assert source is not None, self.error_log(
                "未读取到source（工程文件名）配置"
            )
            assert author is not None, self.error_log("未读取到author（作者）配置")
            assert package is not None, self.error_log("未读取到package（包）配置")
            db = config["db"]
            assert db is not None, self.error_log("未读取到db（数据库）配置")
            assert db["host"] is not None, self.error_log(
                "未读取到db.host（数据库地址）配置"
            )
            assert db["user"] is not None, self.error_log(
                "未读取到db.user（数据库用户）配置"
            )
            assert db["password"] is not None, self.error_log(
                "未读取到db.password（数据库密码）配置"
            )
            assert db["database"] is not None, self.error_log(
                "未读取到db.database（数据库名称）配置"
            )
            init_tenant = config["init_tenant"]
            if init_tenant is not None:
                tenant_id = init_tenant["tenant_id"]
                assert init_tenant["table_prefix"] is not None, self.error_log(
                    "未读取到init_tenant.table_prefix（租户表前缀地址）配置"
                )
                assert init_tenant["delimiter"] is not None, self.error_log(
                    "未读取到init_tenant.delimiter（分隔符）配置"
                )
                assert tenant_id is not None, self.error_log(
                    "未读取到init_tenant.tenant_id（默认租户标识）配置"
                )
                self.print(f"""已开启多租户模式,租户标识[{tenant_id}]""")
            _code_storage = config["code_storage"]
            if _code_storage is not None and _code_storage['enabled'] == 1:
                self.print(f"""已开启源码仓库同步""")
                _root = _code_storage["root"]
                _target = _code_storage["target"]
                assert _root is not None and _target is not None, self.error_log(f"""源码同步配置未设置""")
                assert os.path.exists(_target), self.error_log(
                        f"""同步工程路径不存在"""
                    )
        self.processed += 1

    def run(self, workspace, source):
        self.check_config_file(workspace, source)
        return source
